import { Component } from "@angular/core/";
import { NavController,NavParams } from "ionic-angular";
import { TranslateService } from '@ngx-translate/core';
import { NativeStorage } from '@ionic-native/native-storage';
import { CityPage } from './city/city';
 
@Component({
  selector: 'page-filter',
  templateUrl: 'filter.html',
})
export class FilterPage { 
    selectedItem:number;
    callback:any
    param:any
    info_buy={
        cate:2,
        city:'',
        price_min:'',
        price_max:'',
        distance:'',
        bedroom:'',
        bathroom:'',
        type:'',
        postby:'',
        sortby:'addtime desc',
        keywords:'',
    }
    info_rent={
        cate:1,
        city:'',
        date_min:'',
        date_max:'',
        price_min:'',
        price_max:'',
        distance:'',
        bedroom:'',
        bathroom:'',
        type:'',
        postby:'',
        house_conf:'',
        stu_in:0,
        pet_in:0,
        people_in:'',
        bill:0,
        sortby:'addtime desc',
        keywords:'',
    }
    info_per={
        cate:3,
        city:'',
        date_min:'',
        date_max:'',
        price_min:'',
        price_max:'',
        distance:'',
        bedroom:'',
        bathroom:'',
        type:'',
        postby:'',
        house_conf:'',
        stu_in:0,
        pet_in:0,
        people_in:'',
        bill:0,
        sortby:'addtime desc',
        keywords:'',
    }
    info_service={
        cate:4,
        city:'',
        price_min:'',
        price_max:'',
        date_min:'',
        date_max:'',
        distance:'',
        bedrooms:'',
        bathrooms:'',
        type:'',
        postby:'',
        sortby:'addtime desc',
        keywords:'',
    }
    active={
        type:false,
        postby:false,
        house_conf:false,
        people_in:false,
        sortby:false,
        bedroom:false,
        bathroom:false
    }
    constructor(
        private navCtl: NavController,
        private navParams:NavParams,
        private translate: TranslateService,
        private stroge: NativeStorage
    ) {
       this.selectedItem=1;
       this.callback = this.navParams.get("callback");
       translate.setDefaultLang('en');
    }
    ionViewDidEnter(){
        this.stroge.getItem('lang').then(res=>{
            this.translate.use(res)
        })
    }
    goback(){
        switch(this.selectedItem){
            case 1:{
                this.param = this.info_rent
                break;
            }
            case 2:{
                this.param = this.info_buy
                break;
            }
            case 3:{
                this.param = this.info_per
                break;
            }
            case 4:{
                this.param = this.info_service
                break;
            }
        }
        this.callback(this.param).then(()=>{
            this.navCtl.pop();
        });
    }
    gotocity(){
        this.navCtl.push(CityPage,{callback:this.myCallbackFunction});
    }
    myCallbackFunction=(params) => {
        return new Promise((resolve, reject) => {
            
            if(typeof(params)!='undefined'){
                resolve('ok');
                switch(this.selectedItem){
                    case 1:
                        this.info_rent.city=params;
                        break;
                    case 2:
                        this.info_buy.city=params;
                        break;
                    case 3:
                        this.info_per.city=params;
                        break;
                    case 4:
                        this.info_service.city=params;
                        break;
                    default:
                        console.log('end')
                        break
                }
                console.log('oh my god')
            }else{
                reject(Error('error'))
            }
                
        })
    }
    onSelect(obj:number){
        this.selectedItem = obj;
    }
}